<template>
  <div class="SalSob">
    <div class="sal_title">
      <el-button type="primary" @click="showAddSalary" icon="el-icon-plus">添加工资账套</el-button>
      <el-button type="success" icon="el-icon-refresh" @click="initSalary"></el-button>
    </div>
    <div class="sal_main">
      <el-table :data="salaryData" stripe border style="width: 100%">
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column prop="name" label="账套名称" width="120" align="center">
        </el-table-column>
        <el-table-column prop="basicSalary" label="基本工资" width="80" align="center">
        </el-table-column>
        <el-table-column prop="trafficSalary" label="交通补助" width="80" align="center">
        </el-table-column>
        <el-table-column prop="lunchSalary" label="午餐补助" width="80" align="center">
        </el-table-column>
        <el-table-column prop="bonus" label="奖金" width="70" align="center">
        </el-table-column>
        <el-table-column prop="createDate" label="启用时间" width="110" align="center">
        </el-table-column>
        <el-table-column label="养老金" width="150" align="center">
          <el-table-column prop="pensionPer" label="比率" width="75" align="center">
          </el-table-column>
          <el-table-column prop="pensionBase" label="基数" width="75" align="center">
          </el-table-column>
        </el-table-column>
        <el-table-column label="医疗保险" width="150" align="center">
          <el-table-column prop="medicalPer" label="比率" width="75" align="center">
          </el-table-column>
          <el-table-column prop="medicalBase" label="基数" width="75" align="center">
          </el-table-column>
        </el-table-column>
        <el-table-column label="公积金" width="150" align="center">
          <el-table-column prop="accumulationFundBase" label="比率" width="75" align="center">
          </el-table-column>
          <el-table-column prop="accumulationFundPer" label="基数" width="75" align="center">
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showEditSalaryView(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteSalaryView(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑 -->
    <el-dialog :title="salaryTitle" :visible.sync="dialogVisible" width="50%">
      <div class="add_sal_warp">

        <!-- 步骤 -->

        <el-steps direction="vertical" :active="activeSalary">
          <el-step :title="item" v-for="(item, index) in salaryItemName" :key="index"></el-step>
        </el-steps>

        <el-input class="add_sal_inp" v-model="salary[title]" :placeholder="'请输入' + salaryItemName[index]"
          v-for="(item, title, index) in salary" :key="index" v-show="activeSalary === index"></el-input>

      </div>

      <!-- 按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="preStep">{{
          activeSalary == 10 ? "取 消" : "上一步"
        }}</el-button>
        <el-button type="primary" @click="nextStep">{{
          activeSalary == 10 ? "确 定" : "下一步"
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 步骤默认激活状态
      activeSalary: 0,
      // 工资账套
      salaryData: [],
      // 添加 和 编辑 
      salaryTitle: '',
      dialogVisible: false,
      salaryItemName: [
        "账套名称",
        "基本工资",
        "交通补助",
        "午餐补助",
        "奖金",
        "养老金比率",
        "养老金基数",
        "医疗保险比率",
        "医疗保险基数",
        "公积金比率",
        "公积金基数",
      ],
      // 添加的数据
      salary: {
        id: null,
        name: "",
        basicSalary: 0,
        trafficSalary: 0,
        lunchSalary: 0,
        bonus: 0,
        pensionPer: 0,
        pensionBase: 0,
        medicalPer: 0,
        medicalBase: 0,
        accumulationFundPer: 0,
        accumulationFundBase: 0,
      }
    }
  },
  methods: {
    // 删除
    deleteSalaryView(data) {
      this.$confirm("此操作将永久删除 [" + data.name + "], 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 发送请求
          this.deleteRequest("/salary/sob/"+data.id).then((resp) => {
            if (resp) {
              this.initSalary();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    showEditSalaryView(data) {
      this.salaryTitle = '编辑工资账套';
      this.dialogVisible = true;
      this.activeSalary = 0;
      // 修改数据填充
      this.salary.id = data.id;
      this.salary.name = data.name;
      this.salary.basicSalary = data.basicSalary;
      this.salary.trafficSalary = data.trafficSalary;
      this.salary.lunchSalary = data.lunchSalary;
      this.salary.bonus = data.bonus;
      this.salary.pensionPer = data.pensionPer;
      this.salary.pensionBase = data.pensionBase;
      this.salary.medicalPer = data.medicalPer;
      this.salary.medicalBase = data.medicalBase;
      this.salary.accumulationFundPer = data.accumulationFundPer;
      this.salary.accumulationFundBase = data.accumulationFundBase;
    },
    // 下一步
    nextStep() {
      if (this.activeSalary == 10) {
        // 修改
        if (this.salary.id) {
          this.putRequest('/salary/sob/', this.salary).then(resp => {
            if (resp) {
              this.initSalary();
              this.dialogVisible = false;
            }
          })
        } else {
          this.postRequest('/salary/sob/', this.salary).then(resp => {
            if (resp) {
              this.initSalary();
              this.dialogVisible = false;
            }
          })
        }

       
        return;
      }
      this.activeSalary++;
    },
    // 上一步
    preStep() {

      if (this.activeSalary == 0) {
        this.$message({
          message: '不能再上一步了',
          type: 'warning'
        });
        return;
      }

      if (this.activeSalary == 10) {
        this.dialogVisible = false;
        return;
      }
      this.activeSalary--;
    },
    // 初始化工资账套
    initSalary() {
      this.getRequest('/salary/sob/').then((resp) => {
        if (resp) {
          this.salaryData = resp;
        }
      });
    },
    // 添加工资账套
    showAddSalary() {
      this.salaryTitle = '添加工资账套';
      this.salary = {
        name: "",
        basicSalary: 0,
        trafficSalary: 0,
        lunchSalary: 0,
        bonus: 0,
        pensionPer: 0,
        pensionBase: 0,
        medicalPer: 0,
        medicalBase: 0,
        accumulationFundPer: 0,
        accumulationFundBase: 0,
      }
      this.activeSalary = 0;
      this.dialogVisible = true;

    }
  },
  mounted() {
    this.initSalary();
  }
}
</script>

<style>
.sal_title {
  margin: 10px 0;
  display: flex;
  justify-content: space-between;
}

.add_sal_warp {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.add_sal_inp {
  width: 200px;
}
</style>